package com.crm.system.controller.activityManager;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.qo.activityManager.CustomerGiftActivityAddQO;
import com.crm.model.qo.activityManager.CustomerProductActivityQO;
import com.crm.model.qo.activityManager.CustomerProductGiftActivityQO;
import com.crm.model.vo.activityManager.CustomerProductActivityVO;
import com.crm.model.vo.activityManager.CustomerProductGiftActivityContractVO;
import com.crm.model.vo.activityManager.CustomerProductGiftActivityVO;
import com.crm.model.vo.activityManager.CustomerProductGiftDetailVO;
import com.crm.system.service.activityManager.CustomerProductGiftActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 赠品活动Controller
 * @author luojie
 * @date 2021/3/1 14:54
 */
@RestController
@RequestMapping("/customerProductGiftActivity")
@Slf4j
public class CustomerProductGiftActivityController {

    @Autowired
    private CustomerProductGiftActivityService customerProductGiftActivityService;

    /**
     * 活动管理--赠品活动列表
     * @Author yincuimei
     * @return
     */
    @PostMapping("/selectCustomerProductGiftActivityList")
    public DataResponse selectCustomerProductGiftActivityList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                              @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                              @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                              @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                              CustomerProductGiftActivityQO productGiftActivityQO//查询条件
    ){
        try{
            DhPageInfo<CustomerProductGiftActivityVO> productGiftActivityVOList = customerProductGiftActivityService.selectCustomerProductGiftActivityVOList(productGiftActivityQO,pageIndex,pageSize,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),productGiftActivityVOList);
        }catch(Exception e){
            log.error("赠品活动列表 == customerProductGiftActivity selectCustomerProductGiftActivityList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     *赠品活动列表--(赠品活动上架)根据赠品活动ID查询活动是否在有效期内
     * @param productGiftActivityId  ---赠品活动ID
     * @Author yincuimei
     * @return
     */
    @PostMapping("/selectGiftActivityIsInValidityPeriod")
    public DataResponse selectGiftActivityIsInValidityPeriod(@RequestParam(value="productGiftActivityId")String productGiftActivityId){//赠品活动ID
        try{
            Boolean result = customerProductGiftActivityService.selectGiftActivityIsInValidityPeriod(productGiftActivityId);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("赠品活动上架 == customerProductActivity selectGiftActivityIsInValidityPeriod error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 赠品活动列表--上/下架操作(活动有效期内的可以上架;活动下架没有限制)
     * @Author yincuimei
     * @return
     */
    @PostMapping("/updateGiftActivityShelfStatus")
    public DataResponse updateGiftActivityShelfStatus(@RequestParam(value="productGiftActivityId")String productGiftActivityId,    //赠品活动ID
                                                      @RequestParam(value="giftShelfStatus",required = false)String giftShelfStatus//活动状态--0:上架;1:下架
    ){
        try{
            Boolean result = customerProductGiftActivityService.updateGiftActivityShelfStatus(productGiftActivityId,giftShelfStatus);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("赠品活动上/下架 == customerProductActivity updateGiftActivityShelfStatus error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 添加赠品活动数据
     * @param request
     * @param customerGiftActivityAddQO
     * @author luojie
     * @return
     */
    @PostMapping("/addProductGiftActivity")
    public DataResponse addProductGiftActivity(HttpServletRequest request, @RequestBody CustomerGiftActivityAddQO customerGiftActivityAddQO){
        try{
            int result = customerProductGiftActivityService.addProductGiftActivity(customerGiftActivityAddQO);
            if(result == 1){
                return new DhResponse(ResponseCodeEnum.PRODUCT_GIFT_ACTIVITY.getResponseCode());
            }else if(result == 0){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }else{
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
        }catch(Exception e){
            log.error("新增赠品活动 == customerProductGiftActivity addProductGiftActivity error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 赠品活动列表--查看赠品活动
     * @param productGiftActivityId  ---赠品活动ID
     * @Author yincuimei
     * @return
     */
    @PostMapping("/detailCustomerProductGiftActivity")
    public DataResponse detailCustomerProductGiftActivity(@RequestParam(value="productGiftActivityId")String productGiftActivityId,//赠品活动ID
                                                          @RequestParam(value="giftCategory")String giftCategory,//赠送类别--产品类/价格类
                                                          @RequestParam(value="productCategories")String productCategories){ //产品分类
        try{
            CustomerProductGiftDetailVO productGiftDetailVO = customerProductGiftActivityService.selectCustomerProductGiftActivityDetail(productGiftActivityId,giftCategory,productCategories);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),productGiftDetailVO);
        }catch(Exception e){
            log.error("查看赠品活动 == customerProductGiftActivity detailCustomerProductGiftActivity error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 赠品活动列表--编辑赠品活动
     * @param request
     * @param customerGiftActivityAddQO
     * @author luojie
     * @return
     */
    @PostMapping("/editProductGiftActivity")
    public DataResponse editProductGiftActivity(HttpServletRequest request, @RequestBody CustomerGiftActivityAddQO customerGiftActivityAddQO){
        try{
            int result = customerProductGiftActivityService.editProductGiftActivity(customerGiftActivityAddQO);
            if(result == 1){
                //赠品活动重复
                return new DhResponse(ResponseCodeEnum.PRODUCT_ACTIVITY_EXISTS.getResponseCode());
            }else if(result == 0){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }else{
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
        }catch(Exception e){
            log.error("编辑赠品活动 == customerProductGiftActivity editProductGiftActivity error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 赠品活动列表--删除赠品活动
     * @param productGiftActivityId  ---赠品活动ID
     * @Author yincuimei
     * @return
     */
    @PostMapping("/deleteCustomerProductGiftActivity")
    public DataResponse deleteCustomerProductGiftActivity(HttpServletRequest request,@RequestParam(value="productGiftActivityId")String productGiftActivityId){
        try{
            customerProductGiftActivityService.deleteCustomerProductGiftActivity(productGiftActivityId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }catch(Exception e){
            log.error("删除赠品活动 == customerProductGiftActivity deleteCustomerProductGiftActivity error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 根据产品id获取全部产品下拉框（带该产品选中状态）
     * @param productId 产品id
     * @author luojie
     * @return
     */
    @GetMapping("/selectProductDropDownCheckedListByProductId")
    public DataResponse selectProductDropDownCheckedListByProductId(@RequestParam String productId){
        try{
            List<HashMap> hashMapList = customerProductGiftActivityService.selectProductDropDownCheckedListByProductId(productId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),hashMapList);
        }catch(Exception e){
            log.error("customerProductGiftActivity selectProductDropDownCheckedListByProductId error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 根据产品IdList查询赠品活动
     * @param request
     * @param jsonObject 入参（包括产品idList和价格）
     * @return
     */
    @PostMapping("/selectProductGiftListByProductIdAndPrice")
    public DataResponse selectProductGiftListByProductIdList(HttpServletRequest request,
                                                             @RequestBody JSONObject jsonObject){
        try{
            //产品idList
            JSONArray productIdList = jsonObject.getJSONArray("productIdList");
            //赠品活动最低的价格
            BigDecimal price = jsonObject.getBigDecimal("giftActivityPrice");
            //根据产品idList查询
            List<CustomerProductGiftActivityContractVO> customerProductGiftActivityVOList = customerProductGiftActivityService.selectProductGiftListByProductIdAndPrice(productIdList,price);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductGiftActivityVOList);
        }catch(Exception e){
            log.error("customerProductGiftActivity selectProductGiftListByProductIdAndPrice error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }
}
